﻿var _Scroll_Bar_Width = 13;

function jquery_datagrid_Scrollbar(h, min, max, smallstep, largestep) {
	var _this = this;

	this.position = 0;
	this.positionHandler = new EventManager();
	this.scrollbar = null;
	this.positionChangedCounter = 0;
	this.mouseWheelTargets = [];
	
	this.setHeight = function(h) {
		this.scrollbar.height(h);
		this.scrollbar.find('div').height(max - min + h);
	};
	
	this.setPosition = function(pos) {
		if (this.position == pos) return;
		this.positionChangedCounter++;
		this.position = pos;
		
		var a = this.positionChangedCounter;
		setTimeout(function() {
			if (_this.positionChangedCounter == a) _this.positionHandler.trigger(_this.position);
		}, 100);
	};
	
	this._scrollbarWheel = function (e) {
		e = e ? e : event;
		var ok = false;
		var p = e.target;
		while (p!=null) {
			if($.inArray(p, _this.mouseWheelTargets) > -1) {
				ok = true; break;
			}
			p = p.parentNode;
		}
		if (ok) {
			var dir = e.detail ? e.detail / 3: -e.wheelDelta / 120;
			_this.scrollbar.scrollTop(_this.position + dir * smallstep);
		}
		
		e.returnValue = false;
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
		return false;
	};
	
	this._addEvent = function (o, t, f) {
		if (o.addEventListener) o.addEventListener(t, f, false);
		else if (o.attachEvent) o.attachEvent('on'+ t, f);
		else o['on'+ t] = f;
	};
	
	this._removeEvent = function (o, t, f) {
		if (o.removeEventListener) o.removeEventListener(t, f, false);
		else if (o.detachEvent) o.detachEvent('on'+ t, f);
		else o['on'+ t] = f;
	};
	
	this.addMouseWheelTarget = function(elem) {
		this.mouseWheelTargets.push(elem);
		this._addEvent(elem, "mousewheel", this._scrollbarWheel);
		this._addEvent(document.body, "DOMMouseScroll", this._scrollbarWheel);//fucking FF
	};
	
	this.clearMouseWheelTargets = function() {
		for (var i=0;i<this.mouseWheelTargets.length;i++) {
			this._removeEvent(this.mouseWheelTargets[i], "mousewheel", this._scrollbarWheel);
		}
		this._addEvent(document.body, "DOMMouseScroll", this._scrollbarWheel);//fucking FF
	}
	
	this.init = function() {
		this.scrollbar = $('<div>').css('overflow-x', 'hidden').
			css('overflow-y', 'auto').
			width(15).height(h).
			scroll(function() {
				_this.setPosition(this.scrollTop);
			}).append($('<div>').width(10).height(max - min + h));
		
	};
	this.init();
}

function ScrollHandler(options) {
	var _this = this;
	this.view  = null;
	this.message = null;
	this.pseudotable = null;
	this.scrollbar = null;
	
	this.scroll = function(position) {
		_this.message.html('Showing records from ' + position + ' to ' + (position+options.pageSize-1));
		_this.grid.load(position, position+options.pageSize, function(data) {
			_this.grid.resize();
			_this.scrollbar.setHeight(_this.pseudotable.height() - 3);
		});
	};
	
	
	this.attach = function(grid) {
		this.grid = grid;
		
		this.grid.load(0, 1, function(data) {
			_this.pseudotable = $('<table>').css('width', '100%').addClass('scroll-handler');
			var _tr = _this.pseudotable.get(0).insertRow(-1);
			
			_this.view = $('<div>').addClass('scroll-viewport');
			var td1 = $(_tr.insertCell(-1)).addClass('scroll-view').append(_this.view);
			var td2 = $(_tr.insertCell(-1)).addClass('scroll-cell').width(_Scroll_Bar_Width);
			var td3 = $(_tr.insertCell(-1));
			td1.css('vertical-align', 'top');
			td2.css('vertical-align', 'top');
			
			$(_this.view).append(_this.grid.getContent().children());
			_this.grid.getContent().append(_this.pseudotable);
			
			_this.message = $('<span>').addClass('scroll-position-info');
			_this.grid.getFooter().append(_this.message);
			
			_this.scrollbar = new jquery_datagrid_Scrollbar(td1.height() , 0, data.total - options.pageSize + 1, options.pageSize, options.pageSize );
			td2.append(_this.scrollbar.scrollbar);
			_this.scrollbar.positionHandler.add(function(position) {_this.scroll(position);});
			_this.scrollbar.addMouseWheelTarget(_this.view.get(0));
			
			_this.scroll(0);
		});
	};
	
	this.detach = function() {
		this.message.remove();
		this.scrollbar.clearMouseWheelTargets();
		this.scrollbar = null;
		this.pseudotable.parent().append(_this.view.children());
		this.pseudotable.remove();
	};
}; ScrollHandler.inherits(DatagridPlugin);